package com.jimmy.study.ehcache;

import static org.junit.Assert.assertEquals;

import java.util.List;

import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.jimmy.study.ehcache.young.CacheMethod;

/**
 * 
 * Test using Spring-test
 * 
 * @author Jimmy.Qiu
 * 
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:applicationContext.xml" })
public class SpringCacheTest {
	@Autowired
	CacheManager manager;
	// @Resource
	@Autowired
	CacheMethod method;

	@Test
	public void testGetYoung() {
		// CacheMethod method = new CacheMethod();
		// 这样得到的实例不在Spring的管理之中，也无法Cache
		System.err.println(method.getYoung("hi"));
		System.err.println(method.getYoung("hi"));
		System.err.println(method.getYoung("helo"));
		System.err.println(method.getYoung("helo"));
		System.err.println(method.getYoung("rock"));
		System.err.println(method.getYoung("rock"));

		method.clearYoung();

		System.err.println(method.getYoung("hi"));
		System.err.println(method.getYoung("hi"));
		System.err.println(method.getYoung("helo"));
		System.err.println(method.getYoung("rock"));

		// 输出所有缓存
		String[] names = manager.getCacheNames();
		for (int i = 0; i < names.length; i++) {
			System.err.println("Cache named 'abc':" + names[i]);
		}

		// 得到cache
		Cache abc = manager.getCache("abc");
		System.err.println("Cache named 'abc':" + abc);

		List<String> allkeys = abc.getKeys();
		for (String key : allkeys) {
			System.out.println("KEY : " + key);
		}

		assertEquals("CacheMethod-print-id-rock", abc.getKeys().get(0));
	}
}
